#! /usr/bin/python
# encoding=utf-8
# Created by Fenglu Niu on 2025/5/27 15:36
from typing import Annotated

from fastapi import APIRouter, Depends, Query
from sqlalchemy import func
from sqlmodel import Session, select, col

from api import Page, R
from api.entity import CaseQuery, Case
from api.service.case_apply_record_service import CaseApplyRecordService
from api.service.case_feedback_service import CaseFeedbackService
from api.service.case_flow_service import CaseFlowService
from api.service.case_result_service import CaseResultService
from api.service.person_service import PersonService
from db import get_session

router = APIRouter(prefix='/case', tags=["case"])
SessionDep = Annotated[Session, Depends(get_session)]


async def where_condition(statement, case_query: CaseQuery):
    # 如果有自定义查询字段，请在此自行添加
    if case_query.input_unit_name:
        statement = statement.where(Case.input_unit_name.like(f'%{case_query.input_unit_name}%'))
    if case_query.area_name:
        statement = statement.where(Case.area_name.like(f'%{case_query.area_name}%'))
    if case_query.reg_time_start:
        statement = statement.where(Case.create_time >= case_query.reg_time_start)
    if case_query.reg_time_end:
        statement = statement.where(Case.create_time <= case_query.reg_time_end)
    if case_query.close_time_start:
        statement = statement.where(Case.close_time >= case_query.close_time_start)
    if case_query.close_time_end:
        statement = statement.where(Case.close_time <= case_query.close_time_end)
    if case_query.case_type_name:
        statement = statement.where(Case.case_type_name.like(f'%{case_query.case_type_name}%'))
    if case_query.canal_name:
        statement = statement.where(Case.canal_name == case_query.canal_name)
    if case_query.case_level:
        statement = statement.where(Case.case_level == case_query.case_level)
    if case_query.medi_result_name:
        statement = statement.where(Case.medi_result_name == case_query.medi_result_name)
    if case_query.status_name:
        statement = statement.where(Case.status_name == case_query.status_name)
    if case_query.mediate_unit_name:
        statement = statement.where(Case.mediate_unit_name == case_query.mediate_unit_name)
    if case_query.assist_unit_name:
        statement = statement.where(Case.assist_unit_name == case_query.assist_unit_name)
    if case_query.case_ref:
        statement = statement.where(Case.case_ref == case_query.case_ref)
    if case_query.plaintiffs:
        statement = statement.where(Case.plaintiffs.like(f'%{case_query.plaintiffs}%'))
    if case_query.defendants:
        statement = statement.where(Case.defendants.like(f'%{case_query.defendants}%'))
    if case_query.major_status:
        statement = statement.where(Case.major_status == case_query.major_status)
    if case_query.is_risk:
        statement = statement.where(Case.is_risk == case_query.is_risk)
    if case_query.people_num:
        if case_query.people_num == 1:
            statement = statement.where(Case.people_num >= 1).where(Case.people_num <= 3)
        if case_query.people_num == 2:
            statement = statement.where(Case.people_num >= 4).where(Case.people_num <= 5)
        if case_query.people_num == 3:
            statement = statement.where(Case.people_num >= 6).where(Case.people_num <= 10)
        if case_query.people_num == 4:
            statement = statement.where(Case.people_num >= 11).where(Case.people_num <= 19)
        if case_query.people_num == 5:
            statement = statement.where(Case.people_num >= 20)
    if case_query.amount:
        if case_query.amount == 1:
            statement = statement.where(Case.amount == 0)
        if case_query.amount == 2:
            statement = statement.where(Case.amount < 10000)
        if case_query.amount == 3:
            statement = statement.where(Case.amount >= 10000).where(Case.amount < 100000)
        if case_query.amount == 4:
            statement = statement.where(Case.amount >= 100000).where(Case.amount < 200000)
        if case_query.amount == 5:
            statement = statement.where(Case.amount >= 200000).where(Case.amount < 500000)
        if case_query.amount == 6:
            statement = statement.where(Case.amount >= 500000)
    return statement


@router.post("/list")
async def get_case_list(case_query: CaseQuery, session: SessionDep, current: int = 1,
                        size: Annotated[int, Query(le=100)] = 100):
    # 案件列表查询
    offset, limit = (current - 1) * size, size
    statement = await where_condition(select(Case).offset(offset).limit(limit), case_query)
    cases = session.exec(statement.order_by(Case.id.desc())).all()

    # 查询总数
    count_statement = await where_condition(select(func.count(col(Case.id))), case_query)
    total = session.exec(count_statement).one()

    page = Page.of(current=current, size=size, total=total, records=cases)
    return R.ok(data=page)


@router.get("/person/list")
async def get_case_person(case_id: int, session: SessionDep):
    return R.ok(data=PersonService.list_person(case_id, session))


@router.get("/result")
async def get_case_result(case_id: int, session: SessionDep):
    return R.ok(data=CaseResultService.get_result(case_id, session))


@router.get("/feedback/list")
async def get_case_feedback(case_id: int, session: SessionDep):
    return R.ok(data=CaseFeedbackService.list_feedback(case_id, session))


@router.get("/flow/list")
async def get_case_flow(case_id: int, session: SessionDep):
    return R.ok(data=CaseFlowService.list_flow(case_id, session))


@router.get("/applyRecord/list")
async def get_case_apply_record(case_id: int, session: SessionDep):
    return R.ok(data=CaseApplyRecordService.list_apply_record(case_id, session))
